//
//  YHHoemAgentDetailsInfoViewController.m
//  YHApp
//
//  Created by -- on 2021/7/25.
//

#import "HAHoemAgentDetailsInfoVC.h"
#import "HAHoemAgentDetailsInfoMo.h"
#import "HAHoemAgentMakeShareMoneyVC.h"
#import "HAHoemAgentMakeCashBackVC.h"
#import "HAWebVC.h"

@interface HAHoemAgentDetailsInfoVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, strong) HAHoemAgentDetailsInfoMo *model;


@end

@implementation HAHoemAgentDetailsInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseTLabel.text = @"代理信息";

    [self initUI];
    [self loadData];
    
    // Do any additional setup after loading the view.
}

- (void)initUI{

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(app_topH+44);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

-(void)loadData {
    
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"agent/detail";
    request.parms = @{@"agent_id":self.agent_id?:@""};

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                self.model = [HAHoemAgentDetailsInfoMo mj_objectWithKeyValues:dataModel.data];
                
                [self.dataArray removeAllObjects];
                
                [self.dataArray addObject:@"基本信息"];
                [self.dataArray addObject:@"代理名称"];
                [self.dataArray addObject:@"代理编号"];
                [self.dataArray addObject:@"联系人"];
                [self.dataArray addObject:@"联系电话"];
                
                [self.dataArray addObject:@"结算信息"];
                [self.dataArray addObject:@"结算账户类型"];
                [self.dataArray addObject:@"银行账户名"];
                [self.dataArray addObject:@"银行卡号"];
                [self.dataArray addObject:@"开户银行"];
                [self.dataArray addObject:@"开户支行"];

                [self.dataArray addObject:@"分润规则"];
                [self.dataArray addObject:@"分润模板"];
                [self.dataArray addObject:@"返现规则"];

                [self.tableView reloadData];
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"基本信息"] || [title isEqualToString:@"结算信息"] || [title isEqualToString:@"分润规则"]) {
        return 45;
    }else {
        return 55;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMyMerchantDetailsCellID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMyMerchantDetailsCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([title isEqualToString:@"基本信息"] || [title isEqualToString:@"结算信息"] || [title isEqualToString:@"分润规则"]) {
        [self setCellTitWith:cell.contentView title:title];
    }else if ([title isEqualToString:@"代理名称"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.name isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"代理编号"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.ID isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"联系人"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.name isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"联系电话"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.mobileview isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"结算账户类型"]) {
        NSString *content = @"";
        if (![NSString isEmptyString:self.model.account_bank_account]) {
            if (self.model.type == 1) {
                content = @"公司";
            }else {
                content = @"个人";
            }
        }else {
            content = @"--";
        }
        [self setCellConWith:cell.contentView title:title content:content isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"银行账户名"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.account_surname?:@"--" isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"银行卡号"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.account_bank_account?:@"--" isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"开户银行"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.account_name?:@"--" isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"开户支行"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.account_branch?:@"--" isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"分润模板"]) {
        [self setCellConWith:cell.contentView title:title content:@"修改" isLine:YES isRImg:YES];
    }else if ([title isEqualToString:@"返现规则"]) {
        [self setCellConWith:cell.contentView title:title content:@"" isLine:YES isRImg:YES];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"分润模板"]) {
        HAHoemAgentMakeShareMoneyVC *vc = [[HAHoemAgentMakeShareMoneyVC alloc]init];
        vc.agent_id = self.model.ID;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:@"返现规则"]) {
//        YHHoemAgentMakeCashBackViewController *vc = [[YHHoemAgentMakeCashBackViewController alloc]init];
//        vc.agent_id = self.agent_id;
//        [self.navigationController pushViewController:vc animated:YES];
        HAWebVC *vc = [[HAWebVC alloc]init];
        vc.tit = @"返现规则";
        vc.urlStr = [NSString stringWithFormat:@"%@index/reward/index.html?agent_id=%@&token=%@",kHaBaseUrl,self.agent_id,HAUserDefaults.token];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:@"联系电话"]) {
        if (![NSString isEmptyString:self.model.mobile]) {
            [HAHelper toPhone:self.model.mobile];
        }
    }
}

-(void)setCellTitWith:(UIView *)contentView title:(NSString *)title {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    contentView.backgroundColor = [UIColor khaBGColor];
    
    UILabel *label = [UILabel ha_NLabelWithFont:17 textColor:@"#000000" text:title alignment:1];
    [contentView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(15);
    }];
}


-(void)setCellConWith:(UIView *)contentView title:(NSString *)title content:(NSString *)content isLine:(BOOL)isline isRImg:(BOOL)isRImg {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UILabel *titlabel = [UILabel ha_NLabelWithFont:17 textColor:@"111a34" text:title alignment:0];
    [contentView addSubview:titlabel];
    [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_offset(118);
    }];
    
    if (isRImg) {
        UIImageView *RImgV = [UIImageView ha_createImageView];
        RImgV.image = [UIImage imageNamed:@"ic_cell_more"];
        [contentView addSubview:RImgV];
        [RImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(5);
            make.height.mas_offset(10);
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-20);
        }];
    }
    
    UILabel *conLabel = [UILabel ha_NLabelWithFont:15 textColor:@"858b9c" text:content alignment:2];
    [contentView addSubview:conLabel];
    [conLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(titlabel.mas_right).offset(15);
        if (isRImg) {
            make.right.mas_equalTo(-10-20-10);
        }else {
            make.right.mas_equalTo(-20);
        }
    }];
    
    if ([title isEqualToString:@"联系电话"] && ![NSString isEmptyString:content] && ![NSString isEmptyString:self.model.contactphone]) {
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@  %@",content,@"拨打电话"]];
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#5F67FC"] range:[[attr string]rangeOfString:@"拨打电话"]];
        conLabel.attributedText = attr;
    }
    
    if (isline == YES) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor khaBGColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.height.mas_offset(1);
            make.bottom.mas_equalTo(0);
        }];
    }
}



- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array ];
    }
    return _dataArray;
}
@end
